package org.hxyjs.dao.impl;

import org.hxyjs.dao.MatchOddsRecordDao;
import org.hxyjs.entity.MatchOddsRecord;
import org.hxyjs.mapper.MatchOddsRecordMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("matchOddsRecordDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class MatchOddsRecordDaoImpl implements MatchOddsRecordDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private MatchOddsRecordMapper matchOddsRecordMapper;

    public List<MatchOddsRecord> getAllMatchOddsRecords() throws Exception {
        return matchOddsRecordMapper.selectByExample(null);
    }

    @Override
    public List<MatchOddsRecord> getMatchOddsRecordByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) matchOddsRecordMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return matchOddsRecordMapper.getMatchOddsRecordByPage(map);
    }

//    @Override
//    public MatchOddsRecord selectMatchOddsRecordByMorno(String sno) throws Exception {
//        return matchOddsRecordMapper.selectByPrimaryKey(sno);
//    }


    @Override
    public Integer deleteMatchOddsRecordByMorno(String morno) {
        return matchOddsRecordMapper.deleteByPrimaryKey(morno);
    }

    @Override
    public Integer updateMatchOddsRecordByMorno(MatchOddsRecord matchOddsRecord) {
        return matchOddsRecordMapper.updateByPrimaryKey(matchOddsRecord);
    }

    @Override
    public Integer addMatchOddsRecord(MatchOddsRecord matchOddsRecord) {
        matchOddsRecord.setMorno(initId());
        return matchOddsRecordMapper.insertSelective(matchOddsRecord);
    }
//
//    @Override
//    public MatchOddsRecord selectMatchOddsRecordWithorderAndUser(String morno) throws Exception {
//        return matchOddsRecordMapper.selectMatchOddsRecordWithorderAndUser(morno);
//    }

    public String initId() {
        String maxMorno = matchOddsRecordMapper.getMaxMorno();
        return GenNumTools.initId("m", 3, maxMorno);
    }

    @Override
    public MatchOddsRecord getMatchOddsRecordByMorno(String morno) throws Exception {
        return matchOddsRecordMapper.selectByPrimaryKey(morno);
    }

}